<script type="text/html" template>
    <link rel="stylesheet" href="{{ layui.setter.base }}style/css/reset.css">
    <link rel="stylesheet" href="{{ layui.setter.base }}style/css/home/device.css">
    <link rel="stylesheet" href="{{ layui.setter.base }}style/page.css">
</script>
<style scoped>
  .layui-btn-container .layui-btn{
           font-size: 9px;
           height: 25px;
           line-height: 25px;
       }
   </style>
<!-- 高查 -->
<div class="box layui-fluid layui-row">
    <div class="faultType-header layui-col-md12 layui-btn-container">
        <button class="layui-btn add-faultType layui-btn-radius layui-btn-primary" data-type="addError">
            <i class="layui-icon layui-icon-add-circle" style="font-size: 15px;color: #1E9FFF;"></i>
            <span>新增故障类型</span>
        </button>
    </div>
    <div class="form layui-form layui-col-sm12 layui-col-md12" lay-filter="app-content-comment">
        <div class="layui-form-item layui-col-sm2 layui-col-md2">
            <label class="layui-form-label">故障类型编号:</label>
            <div class="layui-input-block layui-col-space15">
                <input type="text" name="tcBugCode" class="layui-input" list="student_one" id="bugOne"
                    autocomplete="off">
            </div>
        </div>
        <div class="layui-form-item layui-col-sm2 layui-col-md2">
            <label class="layui-form-label">故障类型:</label>
            <div class="layui-input-block">
                <input type="text" name="tcBugName" autocomplete="off" class="layui-input " id="bugTwo"
                    list="student_two">
            </div>
        </div>
        <div class="layui-form-item layui-col-sm2 layui-col-md2 ">
            <label class="layui-form-label ">故障类型说明:</label>
            <div class="layui-input-block ">
                <input type="text" name="tcBugDsc" autocomplete="off" class="layui-input " id="bugThree"
                    list="student_three">
            </div>
        </div>
        <div class="layui-form-item layui-btn-container layui-col-sm2 layui-col-md2"
            style="display: flex;justify-content: flex-end; ">
            <button class="search-btn layui-btn layui-btn-radius layui-btn-primary" lay-submit
                lay-filter="LAY-app-content-comm" id="serachButtons">
                <i class="layui-icon layui-icon-search " style="font-size: 10px; color: #FFF;"></i>
                <span>查询</span>
            </button>
        </div>
    </div>
</div>

<!-- 表格中的操作图标 -->
<script type="text/html" id="erroeButton">
        <i class="layui-icon layui-icon-edit" style="cursor:pointer" lay-event="edit"></i>  
</script>

<div class="layui-fluid">
    <div class="layui-row">
        <div class="tableClass layui-col-md12">
            <table class="layui-table" id="faultTable" lay-filter="faultTable">

            </table>
        </div>
    </div>
</div>

<!-- js部门 -->
<script>
    layui.use(['admin', 'form', 'user', 'table', 'api', 'laytpl', 'utill', 'laydate'], function () { //此段代码必不可少
        var $ = layui.$,
            admin = layui.admin,
            view = layui.view,
            api = layui.api,
            table = layui.table,
            setter = layui.setter,
            laytpl = layui.laytpl,
            utill = layui.utill,
            laydate = layui.laydate,
            form = layui.form;
        // 显示列定制弹出层:完成

        rander();   //调用方法
        function rander() {
         
            // 列定制
            utill.columnCustom('#faultTable', "TcEamReqbug", {
                url: api.host + api.selectBugList,
                where: {
                    tcCatgCode: "",
                    tcCatgName: "",
                    tcBugDsc: ""
                },
                toolbar: {
                    toolbar: '#erroeButton',
                    title: '操作',
                    event: 'detail',
                    align: 'center'
                },
                checkbox: false
            });

            // 高查
            $("#serachButtons").click(function () {
                var bugOne = $("#bugOne").val();
                var bugTwo = $("#bugTwo").val();
                var bugThree = $("#bugThree").val();
                table.reload('#faultTable', {
                    where: {
                        tcCatgCode: bugOne,
                        tcCatgName: bugTwo,
                        tcBugDsc: bugThree
                    },
                    page: {
                        curr: 1
                    }
                })

            });

            table.on('tool(faultTable)', function (obj) {
                var data = obj.data
                if (obj.event === 'edit') {
                    admin.popup({
                        title: '编辑',
                        area: ['325px', '320px'],
                        id: 'layuiadmin-form-tags',
                        success: function (layero, index) {
                            // 页面跳转
                            view(this.id).render('maintain/alert/alert-two', data).done(function () {
                                // 取消按钮
                                $(".btn2").click(function () {
                                    layer.close(index);
                                })
                                // 再一次渲染表格
                                form.render(null, 'faultTable');   // 渲染当期那的表格
                                // 回显效果(成功)
                                form.val("layuiadmin-form-tags", {
                                    "tcBugName": data.tcBugName,
                                    "tcBugDsc": data.tcBugDsc
                                })
                                //监听故障类型
                                form.on('submit(layuiadmin-app-form-submit)', function (data) {
                                    var ones = $("#ones").val();
                                    var twos = $("#twos").val();
                                    if (ones != "" && twos != "") {
                                        // 把数据传递到后台
                                        var field = data.field; //获取提交的字段
                                        field.id = obj.data.id
                                        admin.req({
                                            url: api.host + api.updateBug,
                                            type: 'post',
                                            data: field,
                                            contentType: 'application/json',
                                            dataType: 'json',
                                            data: JSON.stringify(field),
                                            done: function (res) {
                                                rander();   //调用方法 
                                                layer.close(index); //执行关闭 
                                                layer.msg(res.message);
                                            }
                                        });
                                    } else {
                                        layer.msg("修改失败");
                                    }
                                });
                            });
                        }
                    });
                }
            });
        }
        //事件
        var active = {
            // 新增故障类型
            addError: function () {
                admin.popup({
                    title: '新增故障类型',
                    area: ['325px', '330px'],
                    id: 'layuiadmin-form-tags',
                    success: function (layero, index) {
                        view(this.id).render('maintain/alert/add-faultType').done(function () {
                            // 取消按钮
                            $(".btn2").click(function () {
                                layer.close(index);
                            })
                            //监听提交
                            form.on('submit(layuiadmin-app-form-submit)', function (data) {
                                var Ones = $("#Ones").val();
                                var Twos = $("#Twos").val();
                                if (Ones != "" && Twos != "") {
                                    var field = data.field; //获取提交的字段
                                    var fieldOne = JSON.stringify(field);
                                    admin.req({
                                        url: api.host + api.insert,
                                        data: fieldOne,
                                        type: "POST",
                                        dataType: 'json',
                                        contentType: 'application/json',
                                        done: function (res) {
                                            rander();   //调用方法
                                            layer.close(index); //执行关闭 
                                            layer.msg(res.message);
                                        }
                                    });
                                } else {
                                    layer.msg('请填写完成');
                                }
                            });
                            // 取消添加事件
                            $('.layui-form-item').on('click', '.btn2', function () {
                                layer.close(index);
                            });
                        });
                    }
                });
            },
        };
        $('.layui-btn').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
    });
</script>